<?php
// +----------------------------------------------------------------------
// | makePHP
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------

/**
 * 功能: 公用函数
 * 创建日期:Wed Oct 29 12:04:04 CST 2008
 * 最后更新:
 * 作者: sanshi
 */

/**
 * 错误处理函数，页面跳转
 * @author sanshi
 * @param string $page  跳转页面
 * @param string $msg   提示信息
 * @param  int   $h     跳转时间
 */
function jumpPage($page,$msg='',$h=0)
{
	//$msg = empty($msg)?'':"?msg=".base64_encode($msg);
	$msg = urlencode($msg);
	$msg = empty($msg)?'':"?msg={$msg}";
	$url=$page.$msg;
	if(!@header("Location:".$url))
	{
	   echo "<meta http-equiv=refresh content='{$h};url=".$url."'>";
	}
	exit();
}
/**
 * 日志记录函数
 * @name   write_log
 * @param  string	log_info 	 日志描述
 * @param  string	log_type 	 日志级别
 * @param  string	log_error_no 错误号，0为没有
 * @param  string	file_name 	 发生问题的文件名，可以为空
 * @return bool 	false	写入信息
 * @access public
 * @see
 * @version	1.0.0 (Wed Oct 29 11:09:03 CST 2008)
 * @author	sanshi
 */
function write_log ($log_info,$log_type,$log_error_no=0,$file_name="")
{

	return $false;
}
/**
 * 错误信息处理
 * @name jumpError
 * @param  int	error_no 	错误号
  * @param  int	$jump_url 	转到的url
 * @return
 * @access public
 * @see
 * @version	1.0.0 (Wed Oct 29 11:06:28 CST 2008)
 * @author	sanshi
 */
function jumpError($error_no,$jump_url="",$is_admin=1)
{
	if($error_no<= -2000)
	{
		//采取js输出
		alertMsg($error_no,$jump_url);
	}else{
		global $error_object;
		//取得错误信息
		$msg = $error_object->getErrorMsg($error_no);
		if($is_admin)
		{
			$page = empty($jump_url)?ADMIN_HOST_PATH.'error.php':$jump_url;
		}else{
			$page = empty($jump_url)?HOST_PATH.'error.php':$jump_url;
		}
		//采取页面跳转
		jumpPage($page,$msg,$h=0);
	}
}
//信息提示，返回项目首页
function goToIndex($error_no)
{
	global $error_object;
	//取得错误信息
	$msg = $error_object->getErrorMsg($error_no);
	echo "<script>";
	echo "alert(' {$msg} ');";
	echo "window.location='".HOST_PATH."index.php';";
	echo "</script>";
	exit();
}
//信息提示，如果给跳转页面就跳到指定页，如果不提供，直接跳转到上一页
function alertMsg($error_no,$url = '')
{
	global $error_object;
	//取得错误信息
	$msg = $error_object->getErrorMsg($error_no);
	echo "<script>";
	echo "alert(' {$msg} ');";
	echo true == empty($url) ? "history.go(-1);" :  "window.location='{$url}';";
	echo "</script>";
	exit();
}
//信息提示，如果给跳转页面就跳到指定页，如果不提供，直接跳转到上一页
function alertMsg2($msg,$url = '')
{
	echo "<script>";
	echo "alert(' {$msg} ');";
	echo true == empty($url) ? "history.go(-1);" :  "window.location='{$url}';";
	echo "</script>";
	exit();
}
function alertMsg3($msg,$url = '')
{
	echo "<script>";
	//echo "self.parent.tb_remove();";
	echo "alert(' {$msg} ');";
	echo true == empty($url) ? "history.go(-1);" :  "window.location='{$url}';";
	echo "</script>";
	exit();
}

function getUserIP()
{
	if(isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
		$user_ip=$_SERVER["HTTP_X_FORWARDED_FOR"];
	} else {
		$user_ip=$_SERVER["REMOTE_ADDR"];
	}
	return $user_ip;
}

//截取字符串
function cutstr($string, $length, $dot = ' ...', $charset='utf-8') {
	if(strlen($string) <= $length) {
		return $string;
	}

	$string = str_replace(array('&amp;', '&quot;', '&lt;', '&gt;'), array('&', '"', '<', '>'), $string);

	$strcut = '';
	if(strtolower($charset) == 'utf-8') {

		$n = $tn = $noc = 0;
		while($n < strlen($string)) {

			$t = ord($string[$n]);
			if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
				$tn = 1; $n++; $noc++;
			} elseif(194 <= $t && $t <= 223) {
				$tn = 2; $n += 2; $noc += 2;
			} elseif(224 <= $t && $t < 239) {
				$tn = 3; $n += 3; $noc += 2;
			} elseif(240 <= $t && $t <= 247) {
				$tn = 4; $n += 4; $noc += 2;
			} elseif(248 <= $t && $t <= 251) {
				$tn = 5; $n += 5; $noc += 2;
			} elseif($t == 252 || $t == 253) {
				$tn = 6; $n += 6; $noc += 2;
			} else {
				$n++;
			}

			if($noc >= $length) {
				break;
			}

		}
		if($noc > $length) {
			$n -= $tn;
		}

		$strcut = substr($string, 0, $n);

	} else {
		for($i = 0; $i < $length; $i++) {
			$strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
		}
	}

	$strcut = str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $strcut);

	return $strcut.$dot;
}

//计算权限值 用户突破32位限制
function parse_power($user_power,$method_power)
{
	return  $user_power & $method_power;
}

/**
 *
 *
 * @author slash
* @copyright slash, 19 四月, 2010
 * @package default
 **/

/**
 * Define 将传入的内容压缩成6位字母
 */
function shorturl($input) {
  $base32 = array (
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
    'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
    'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
    'y', 'z', '0', '1', '2', '3', '4', '5'
    );

  $hex = md5($input);
  $hexLen = strlen($hex);
  $subHexLen = $hexLen / 8;
  $output = array();

  for ($i = 0; $i < $subHexLen; $i++) {
    $subHex = substr ($hex, $i * 8, 8);
    $int = 0x3FFFFFFF & (1 * ('0x'.$subHex));
    $out = '';

    for ($j = 0; $j < 6; $j++) {
      $val = 0x0000001F & $int;
      $out .= $base32[$val];
      $int = $int >> 5;
    }

    $output[] = $out;
  }

  return $output;
}
/**
 * 返回相关服务项目
 *
 * @return unknown
 */
function get_service()
{
	global $service_array;
	return $service_array;
}

?>
